const app = getApp();
const util = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        SiteUrl: app.globalData.SiteUrl,
        storeList: [],
        storeClassList: [],
        address: '',
        latitude: '',
        longitude: '',
        keyword: '',
        curpage: 1,
        sc_id: 0,
        hasmore: true,
        loadAnimate: true,
        active: true,
        topNum: 0,
        floorstatus: false
    },
    onLoad: function (options) {
        //请求分享数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
            data: {},
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == '200') {
                    this.setData({
                        share_default_title: res.data.datas.title,
                        share_default_image_url: res.data.datas.imageUrl
                    })
                }
            }
        })
        var self = this;
        const query = wx.createSelectorQuery()
        query.select('.store_list_view').boundingClientRect(function (res) {
            self.setData({
                storeViewHeight: res.top,
                screenHeight: app.globalData.wh
            })
        })
        query.exec();
        util.share();
        var self = this;
        wx.getStorage({
            key: 'latitude',
            success: function (res) {
                self.setData({
                    latitude: res.data
                })
            },
        })
        wx.getStorage({
            key: 'longitude',
            success: function (res) {
                self.setData({
                    longitude: res.data
                })
            },
        })
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    key: res.data
                });
            }
        })
    },
    chooseClass: function (e) {
        this.setData({
            sc_id: e.currentTarget.dataset['scid'],
            storeList: [],
            curpage: 1,
            hasmore: true,
            loadAnimate: true
        });
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=store_list',
            data: {
                curpage: self.data.curpage,
                latitude: self.data.latitude,
                longitude: self.data.longitude,
                sc_id: self.data.sc_id
            },
            success(res) {
                self.setData({
                    storeList: res.data.datas.store_list
                })
                if (!res.data.hasmore) {
                    self.setData({
                        loadAnimate: false
                    })
                }
            }
        })
    },
    onReady: function () {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=store_list',
            data: {
                curpage: self.data.curpage,
                latitude: self.data.latitude,
                longitude: self.data.longitude,
                sc_id: self.data.sc_id,
                keyword: self.data.keyword
            },
            success(res) {
                if (res.data.code == 200) {
                    self.setData({
                        storeList: res.data.datas.store_list,
                        storeClassList: res.data.datas.store_class_list,
                        hasmore: res.data.hasmore
                    })
                }
            }
        })
    },
    onShow: function () {
        util.tabCalCartNum();
        var self = this;
        wx.getStorage({
            key: 'address',
            success: function (res) {
                self.setData({
                    address: res.data
                })
            },
        })
    },
    storeScrollLower: function () {
        var self = this;
        if (this.data.hasmore) {
            this.data.curpage++;
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=store_list',
                data: {
                    curpage: self.data.curpage,
                    latitude: self.data.latitude,
                    longitude: self.data.longitude,
                    sc_id: self.data.sc_id,
                    keyword: self.data.keyword
                },
                success(res) {
                    self.setData({
                        storeList: self.data.storeList.concat(res.data.datas.store_list),
                        hasmore: res.data.hasmore
                    })
                }
            })
        } else {
            this.setData({
                loadAnimate: false
            })
        }
    },
    getInputWords: function (e) {
        this.setData({
            keyword: e.detail.value
        })
    },
    searchStore: function () {
        this.setData({
            storeList: [],
            curpage: 1,
            hasmore: true,
            loadAnimate: true
        });
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=store_list',
            data: {
                curpage: self.data.curpage,
                latitude: self.data.latitude,
                longitude: self.data.longitude,
                sc_id: self.data.sc_id,
                keyword: self.data.keyword
            },
            success(res) {
                if (res.data.code == 200) {
                    self.setData({
                        storeList: res.data.datas.store_list,
                        storeClassList: res.data.datas.store_class_list,
                        hasmore: res.data.hasmore
                    })
                }
                self.setData({
                    loadAnimate: false
                })
            }
        })
    },
    viewScroll: function (e) {
        if (e.detail.scrollTop > 100) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
    //回到顶部
    toTop: function (e) { // 一键回到顶部
        this.setData({
            topNum: 0
        });
    },
    // onPullDownRefresh: function () {
    //     this.onReady();
    // },
    onShareAppMessage: function () {
        let share_key = this.data.key ? this.data.key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.share_default_title,
            imageUrl: this.data.share_default_image_url,
            path: share_path
        }
    }
})